<?php

namespace app\data\command;

use app\data\model\ShopOrder;
use app\data\model\ShopOrderItem;
use app\data\service\OrderService;
use think\admin\Command;
use think\admin\Exception;
use think\console\Input;
use think\console\Output;
use think\Model;

/**
 * 发货15天后自动确认收货
 * Class OrderClean
 * @package app\data\command
 */
class DeleteOrder extends Command
{
    protected function configure()
    {
        $this->setName('xdata:DeleteOrder');
        $this->setDescription('删除指定日期后的未支付订单');
    }

    /**
     * 业务指令执行
     * @param Input $input
     * @param Output $output
     * @return void
     * @throws Exception
     */
    protected function execute(Input $input, Output $output)
    {
        $this->_autoDeleteOrder();
    }
    /**
     * 取消订单会删除
     */
    private function _autoDeleteOrder()
    {
        try {
            $length_time = sysconf('can_deletetime');
            if(!isset($length_time)) $length_time = 7;
            $map = [['status', '=', 0],['payment_status', '=', 0]];
            $map[] = ['create_at', '<', date('Y-m-d H:i:s', strtotime('-'.$length_time.' days'))];
            [$count, $total] = [0, ($result = ShopOrder::mk()->where($map)->select())->count()];
            $result->map(function (Model $item) use ($total, &$count) {
                $this->queue->message($total, ++$count, "开始删除订单 {$item['order_no']}");
                $opera[] = $item->delete();
                sysoplog('确认订单','系统：自动确认订单：'.$item['order_no']);
                $this->queue->message($total, $count, "完成确认订单 {$item['order_no']}", 1);
            });
        } catch (\Exception $exception) {
            $this->queue->error($exception->getMessage());
        }
    }
}
